const { check, validationResult } = require('express-validator');//数据验证
const router = require('express').Router({

});

const rootControllers = require("../../controllers/rootControllers");
const authorization = require("../../modules/authorization");

router.get(
    '/',
    async function (req, res, next) {
        let data = await rootControllers.selectRolesList();
        let temp = {};
        for (let item of data) {
            if( temp[item.role_id]===undefined ){
                temp[item.role_id] = {
                    role_id: item.role_id,
                    role_name: item.role_name,
                    rights:[]
                }
            }
            let tempRight = {
                right_id: item.right_id,
                right_name: item.right_name,
                // children:[]
                right_function:item.right_function
            }
            // if(item.right_function&1){
            //     tempRight.children.push({
            //         right_id:item.right_id+"0001",
            //         right_name:"GET:查询"
            //     })
            // }
            // if(item.right_function&2){
            //     tempRight.children.push({
            //         right_id:item.right_id+"0010",
            //         right_name:"GET:添加"
            //     })
            // }
            // if(item.right_function&4){
            //     tempRight.children.push({
            //         right_id:item.right_id+"0100",
            //         right_name:"GET:更新"
            //     })
            // }
            // if(item.right_function&8){
            //     tempRight.children.push({
            //         right_id:item.right_id+"1000",
            //         right_name:"GET:删除"
            //     })
            // }
            temp[item.role_id]["rights"].push(tempRight);
        }
        res.sendResult(Object.values(temp), 200, "请求成功");
    }
);

router.post(
    '/',
    [
        check('role_name').notEmpty().withMessage('role_name参数不能为空'),
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }
        let data = await rootControllers.selectRoleByName(req.body.role_name);
        if(data){
            res.sendResult(null, 400, "添加失败,角色名已存在");
            return;
        }
        await  rootControllers.insertRole(req.body.role_name);
        res.sendResult({}, 201, "添加成功");
    }
);

router.put(
    '/',
    [
        check('role_id').notEmpty().withMessage('role_id参数不能为空'),
        check('right_id').notEmpty().withMessage('right_id参数不能为空'),
        check('right_function').notEmpty().withMessage('right_function参数不能为空')
    ],
    async function (req, res, next) {
        const errors = validationResult(req)
        if (!errors.isEmpty()) {
            return res.sendResult(null, 400, errors.array()[0].msg);
        }
        let data = await rootControllers.deleteRoleRight(req.body.role_id,req.body.right_id);
        if(data){
            res.sendResult(null, 400, "更新权限失败");
            return;
        }
        if(req.body.right_function!==0){
            await  rootControllers.insertRoleRight(req.body.role_id,req.body.right_id,req.body.right_function);
        }
        authorization.invocation("/api/v1")
        res.sendResult({}, 201, "更新权限成功");
    }
);

module.exports = router;